package com.tim.test.download.house;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import groovy.util.logging.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * jQuery选择器规则，https://www.runoob.com/jquery/jquery-ref-selectors.html：
 * 	.选择class，#选择id，[]选择属性，:contains(第)内容包含‘第’
 * 	p,h2；同时选择p、h2
 * 	div.main；同时满足
 * 	div .comment；子孙
 * 	div>p；子级
 * 	div~h1；兄弟
 */
@Slf4j
public class AnJuKe {
    String dir = "D:/个人资料/house/";
    String houseSourceFile =dir+"房源信息.csv";
    String appName=this.getClass().getSimpleName().toLowerCase();
    String appDir = dir+appName+"/";
    String appHtmlDir = appDir +"html/";
    String erShouFangFile = appDir +"erShouFang.csv";
    Set<String> houseSet = new TreeSet<>();
    String xiaoXue="没得小学";
    String xiaoQu="XX小区";
    String urlPre="https://chongqing.anjuke.com/sale/rd1/?q=";
    int appUrlIndex=3;


	@Test
	public void downloadByPage() throws Exception {
        List<String> houseSourceList = Files.readAllLines(Paths.get(houseSourceFile), Charset.forName("GB2312"));
        Path erShouFangFilePath = Paths.get(erShouFangFile);
        List<String> houseList = Files.readAllLines(erShouFangFilePath, Charset.forName("GB2312"));
        houseSet.addAll(houseList);
        String dateStr = DateUtil.formatDate(new Date());

        for (int i = 1; i < houseSourceList.size(); i++) {
            // 解析地址
            String[] split = houseSourceList.get(i).trim().split(",");
            xiaoXue =split[0].trim();
            xiaoQu =split[1].trim();
            String houseSourceUrl = urlPre+ URLEncoder.encode(split[appUrlIndex].trim());

            try {
                Document document = Jsoup.connect(houseSourceUrl).get();
                Files.write(Paths.get(appHtmlDir, String.join("_",xiaoXue, xiaoQu, dateStr)), Arrays.asList(document.html()));
                Elements scripts = document.getElementsByTag("script");
                for(Element script : scripts){
                    String scriptHtml = script.html().trim();
                    if(scriptHtml.startsWith("window.GLOBAL_INFOS")){
                        Set<String> set = parseErShouFang(scriptHtml);
                        houseSet.addAll(set);
                        Files.write(erShouFangFilePath, houseSet);
                        System.out.println(String.format("下载成功，小学：%s，小区：%s，成交信息：%s", xiaoXue, xiaoQu, houseSet));
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            Thread.sleep(10*1000);
        }

        System.out.println("SUCCESS");

	}


    @Test
    public void t() throws IOException {
        String str=Files.readAllLines(Paths.get(appDir, "anjuke_script.txt"))
                .stream().collect(Collectors.joining());
        Set<String> set = parseErShouFang(str);
        System.out.println("Variable ershoufang: " + JSON.toJSONString(set));
    }

    public Set<String> parseErShouFang(String info) throws IOException {
        info=info.replaceFirst("window\\.","")
                .replaceAll(",\\s+// sidebar:null","");
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("nashorn");

        try {
            // 执行脚本
            engine.eval(info);

            // 获取变量值
            Object globalInfos = engine.get("__NUXT__");
//            System.out.println("Variable globalInfos: " + JSON.toJSONString(globalInfos));
            JSONObject global = JSON.parseObject(JSON.toJSONString(globalInfos));
            JSONObject ershoufang = global.getJSONObject("data")
                    .getJSONObject("list")
                    .getJSONObject("list");
            Set<String> set = ershoufang.keySet()
                    .stream()
                    .map(key -> {
                        JSONObject house = ershoufang.getJSONObject(key)
                                .getJSONObject("info")
                                .getJSONObject("property");
                        JSONObject attribute = house.getJSONObject("attribute");
                        /*
                            ,"title":"百年世家     2室1厅1卫"
                            ,"community_id":"100420767"
                            ,"id":"3159740235605001"
                            ,"attribute":
                                ,"use_type":"普通住宅"
                                ,"area_num":"64"
                                ,"avg_price":"10860"
                                ,"price":"69.5"
                                ,"floor_level":"中层(共30层)"
                        * */
                        return String.join(",", xiaoXue
                                , xiaoQu
                                , attribute.getString("area_num")
                                , attribute.getString("price")
                                , house.getString("title")
                                , attribute.getString("use_type")
                                , attribute.getString("avg_price")
                                , attribute.getString("floor_level")
                                , house.getString("community_id")
                                , house.getString("id")
                        );
                    }).collect(Collectors.toSet());
            return set;
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return null;
    }

}
